package com.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.commons.ServerResponse;
import com.stu.mapper.CartMapper;
import com.stu.pojo.Cart;
import com.stu.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ICartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
    @Autowired
    private CartMapper cartMapper;
    @Override
    public Integer getUserCartProductCount(Integer userId) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_Id",userId);
        wrapper.orderByAsc("user_Id");
        Integer integer = Math.toIntExact(cartMapper.selectCount(wrapper));
        return integer;
    }


    @Override
    public ServerResponse savecart(Cart cart) {
        int rows = cartMapper.insert(cart);
        if (rows>0){
            return ServerResponse.success("添加成功",cart);
        }else{
            return ServerResponse.success("添加失败",null);
        }
    }
}
